package com.wb.oldwomen;

import android.util.Log;

import com.wb.fw.GateActivity;
import com.wb.fw.Global;
import com.wb.fw.IConstants;
import com.wb.fw.SoundManager;
import com.wb.fw.Util;
import com.wb.fw.WorldCamera;
import com.wb.oldwomen.module.Enemy;
import com.wb.oldwomen.module.HitControl;
import com.wb.oldwomen.module.ObjControl;
import com.wb.oldwomen.module.Oldwoman;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.ColorLayer;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.nodes.TiledSprite;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.engine.utils.TargetSelector;

public class GamePlayLayer extends ColorLayer implements Ioldwomam {
	public static GamePlayLayer make(GameScene parent) {
		return new GamePlayLayer(parent);
	}
	GameScene mparent;
	TiledSprite mfar, msnowhill, mnearhill, mroad;
	Label mscore;
	WorldCamera mcamera;
	Texture2D mroadsprite;

	Button mbtnJump;
	boolean mstarted;
	public Enemy menemy;
	public Oldwoman mwoman;
	
	////////
	HitControl mhitcontrol;
	ObjControl mobjControl;
	int mcountGold, mcountApple;
	
	protected GamePlayLayer(GameScene parent) {
		mparent = parent;
		setKeyEnabled(true);
		setTouchEnabled(true);
		
//		mcamera = Camera.make();
//		mcamerax = Global.sWYSize.width/2;
//		mcameray = Global.sWYSize.height/2;
//		mcamera.setCenter(mcamerax, mcameray, 0);
		//////////////背景和按钮///////////////
		initBG();
		
		initUI();
		
		////////
		mhitcontrol = HitControl.getInstance(this);
		mobjControl = ObjControl.getInstance(this);

		///////////敌人和主角/////
		initObj();
	}


	private void initObj() {
		mwoman = Oldwoman.make(this);
		menemy = Enemy.make(this);
		
		mcamera = WorldCamera.getInstance();
	}

	private void initUI() {
		//jump
		mbtnJump = Button.make(R.drawable.igm_jump_n, R.drawable.igm_jump_p,
				R.drawable.igm_jump_n, R.drawable.igm_jump_n,
				new TargetSelector(this, "xlHoldDown", null),
				new TargetSelector(this, "xlHoldUp", null));
		mbtnJump.autoRelease();
		mbtnJump.setScale(Global.sScaleRate);
		mbtnJump.setPosition(Global.sStartX+700*Global.sScaleRate, Global.sStartY-405*Global.sScaleRate);
		addChild(mbtnJump, Z_IGM_BTN);
		
		////////////////////////////
		Button btnPause = Button.make(R.drawable.img_pause, R.drawable.img_pause, this, "xlPause");
		btnPause.autoRelease();
		btnPause.setScale(Global.sScaleRate);
		btnPause.setPosition(Global.sStartX + 40*Global.sScaleRate, Global.sStartY - 40*Global.sScaleRate);
		addChild(btnPause, Z_IGM_BTN);
		
		Button btnAgain = Button.make(R.drawable.igm_again, R.drawable.igm_again, this, "xlAgain");
		btnAgain.autoRelease();
		btnAgain.setScale(Global.sScaleRate);
		btnAgain.setPosition(Global.sEndX - 40*Global.sScaleRate, Global.sStartY - 40*Global.sScaleRate);
		addChild(btnAgain, Z_IGM_BTN);
		
		Sprite bg_score = Sprite.make(R.drawable.img_score);
		bg_score.autoRelease();
		bg_score.setScale(Global.sScaleRate);
		bg_score.setPosition(Global.sWYSize.width/2, Global.sStartY - 40*Global.sScaleRate);
		addChild(bg_score, Z_IGM_BTN);
		
		float offsetw = (bg_score.getWidth()/2-10)*Global.sScaleRate;
		mscore = Label.make("0", 35);
		mscore.autoRelease();
		mscore.setScale(Global.sScaleRate);
		mscore.setAnchor(1f, 0.5f);
		mscore.setPosition(Global.sWYSize.width/2 + offsetw, Global.sStartY - 40*Global.sScaleRate);
		addChild(mscore, Z_IGM_BTN);
		//////////////////////////////////////////////
	}

	private void initBG() {
		Texture2D farsprite = Texture2D.makePNG(R.drawable.bg_far);
		farsprite.autoRelease();
		mfar = TiledSprite.make(farsprite);
		mfar.autoRelease(true);
		mfar.setAnchor(0, 1);
//		mfar.setContentSize(mfar.getWidth()*Global.sScaleRate, mfar.getHeight()*Global.sScaleRate);
//		mfar.setPosition(Global.sStartX, Global.sStartY);
		mfar.setTileDirection(true, false);
		mfar.setScale(Global.sScaleRate);
		addChild(mfar);
		
		Texture2D snowhillsprite = Texture2D.makePNG(R.drawable.bg_snow_hill);
		snowhillsprite.autoRelease();
		msnowhill = TiledSprite.make(snowhillsprite);
		msnowhill.autoRelease();
		msnowhill.setTileDirection(true, false);
//		msnowhill.setPosition(Global.sWYSize.width/2, Global.sWYSize.height/2);
		msnowhill.setScale(Global.sScaleRate);
		msnowhill.setSpacing(Global.sWYSize.width, 0);
		addChild(msnowhill);
		
		mroadsprite = Texture2D.makePNG(R.drawable.road);
		mroadsprite.autoRelease();
		
		Texture2D nearhillsprite = Texture2D.makePNG(R.drawable.bg_near_hill);
		nearhillsprite.autoRelease();
		mnearhill = TiledSprite.make(nearhillsprite);
		mnearhill.autoRelease();
		mnearhill.setScale(Global.sScaleRate);
		mnearhill.setTileDirection(true, false);
		mnearhill.setAnchor(0, 0.5f);
//		mnearhill.setPosition(Global.sStartX, Global.sEndY + nearhillsprite.getHeight()/3*Global.sScaleRate + roadsprite.getHeight()*Global.sScaleRate);
		addChild(mnearhill);
		
		mroad = TiledSprite.make(mroadsprite);
		mroad.autoRelease();
		mroad.setScale(Global.sScaleRate);
		mroad.setTileDirection(true, false);
		mroad.setAnchor(0, 0.5f);
//		mroad.setPosition(Global.sStartX, Global.sEndY+roadsprite.getHeight()/2*Global.sScaleRate);
		addChild(mroad, Z_ROAD);
	}

	public void clearLevelData() {
		mstarted = false;	
		menemy.clearLevelData();
		mwoman.clearLevelData();
		
		mobjControl.clearLevelData();
		mcamera.resetCamera();
	}

	public void setLevelData() {
		mcountApple = 0;
		mcountGold = Util.getGold(GateActivity.sInstance);
		
		mfar.setPosition(Global.sStartX, Global.sStartY);
		msnowhill.setPosition(Global.sWYSize.width/2, Global.sWYSize.height/2);
		mroad.setPosition(Global.sStartX, Global.sEndY+mroadsprite.getHeight()/2*Global.sScaleRate);
		mnearhill.setPosition(Global.sStartX, Global.sEndY + mroadsprite.getHeight()/3*Global.sScaleRate + mroadsprite.getHeight()*Global.sScaleRate);
		
		mstarted = false;
		menemy.setLevelData(mparent.getCurLevel());
		mwoman.setLevelData(mparent.getCurLevel());
		if (mparent.getCurLevel() == 0) {
			mhitcontrol.setHitVisible(true, HIT_GET_APPLE, WYPoint.make(Global.sStartX+250*Global.sScaleRate, Global.sStartY-125*Global.sScaleRate));
			mhitcontrol.setHitVisible(true, HIT_BEFORE_XIAOTOU, WYPoint.make(Global.sStartX+550*Global.sScaleRate, Global.sStartY-210*Global.sScaleRate));
		}
		
		mobjControl.setLevelData(mparent.getCurLevel());
		mcamera.setTarget(mwoman, WOMAN_FOCUS_X);
		mscore.setText("0");
	}
	
	public final int getCurLevel() {
		return mparent.getCurLevel();
	}

	/**
	 * 25fps,40ms.speed:8px/fps,1s=200px...1m=12000px
	 * @param delta
	 */
	public void update(float delta) {
		if (!mstarted) {
			return;
		}
		/////////////////////////////////
		mcamera.update(delta);
		float offset = -mcamera.getFrameOffsetX();
		mfar.offsetBy(offset/50f,0);
		msnowhill.offsetBy(offset/40f,0);
		mnearhill.offsetBy(offset/20f, 0);
		mroad.offsetBy(offset, 0);
		
		mhitcontrol.update(delta);
		mobjControl.update(delta);
		////////////////////////////////
		mwoman.update(delta);
		menemy.update(delta);

	}
	
	
////////////////上面是模板/////////////////////////////
	public void xlHoldDown() {
		if (mstarted) {
			mwoman.btnDown();
		} else {
			mstarted = true;
			menemy.startRun();
			mwoman.startRun();
			if (mparent.getCurLevel() == 0) {
				mhitcontrol.setHitVisible(false, HIT_GET_APPLE, null);
				mhitcontrol.setHitVisible(false, HIT_BEFORE_XIAOTOU, null);
			}
		}
	}
	
	public void xlHoldUp() {
		if (!mstarted)
			return;
		mwoman.btnUp();
	}
	
	public void xlPause() {
		SoundManager.clickMenuItem();
		mparent.switchState(GS_GAME_IGM);
	}
	
	public void xlAgain() {
		SoundManager.clickMenuItem();
		clearLevelData();
		setLevelData();
	}
	
	public void getBonus(int bonus) {
		Log.d("xltest", "getbonus="+bonus);
		switch(bonus) {
		case BONUS_APPLE:
			mcountApple += 1;
			SoundManager.playEffect(R.raw.apple);
			break;
		case BONUS_GOLD:
			mcountGold += 1;
			SoundManager.playEffect(R.raw.coin0);
			break;
		}
		int score = mcountApple*300+mcountGold*50;
		mscore.setText(String.valueOf(score));
	}
	

	@Override
	protected boolean onBackButton() {
		if (isVisible()) {
			mparent.switchState(GS_GAME_IGM);
		}
		return true;
	}
	
	ResultListener mResultListener;
	public void showResult() {
		if (mResultListener != null) {
			mResultListener.onresult(mcountApple, mcountGold);
		}
	}
	
	public void setResultListener(ResultListener l) {
		mResultListener = l;
	}

	public interface ResultListener {
		public void onresult(int...result);
	}
	//////////////////////
}
